<?php

namespace app\api\controller\evaluation;

use app\admin\model\EvaluationElist;
use app\admin\model\EvaluationQrcode;
use app\admin\model\EvaluationTemplate;
use app\common\controller\ApiController;
use think\App;
header('Access-Control-Allow-Origin:*');
ini_set("max_execution_time", 300);

class Template extends ApiController
{

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\EvaluationTemplate();

    }

    /**
     * Notes: 模板列表
     * author: bxm
     * Date: 2023/2/18
     * Time: 15:25
     * @param $qr_user_id
     */
    public function TemplateList($users_id)
    {

        $list = EvaluationQrcode::order('id','desc')->select();
        foreach ($list as $v){
            $v->template_name = EvaluationTemplate::where('id',$v->template_id)->value('name') ?? '';
            $v->type = 1;//去评价
            $exist = EvaluationElist::where('qr_id',$v->id)->where('users_id',$users_id)->find();
            if(!empty($exist)){
                $v->type = 2;//去追评
            }
        }
        $res = array('code' => '0', 'msg' => '上传成功!', 'data' => $list);
        echo json_encode($res);
    }

    public function getInfo($id){

        $res = EvaluationTemplate::find($id)->toArray();

        $data = array('code' => '0', 'msg' => '操作成功!', 'data' => $res);
        echo json_encode($data);
    }


    /**
     * Notes: 自动生成二维码
     * author: bxm
     * Date: 2023/2/21
     * Time: 9:27
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function autoAddTemplate()
    {
        try{
            $post = request()->post();
            $this->yanzSigin($post);
            if(!isset($post['data']['template_id']) && empty($post['data']['template_id'])){
                $data = array('code' => '1', 'msg' => '模板id不能为空!', 'data' => []);
                return json_encode($data);
            }
            if(!isset($post['data']['qr_user_id']) && empty($post['data']['qr_user_id'])){
                $data = array('code' => '1', 'msg' => '生成二维码普通用户id不能为空!', 'data' => []);
                return json_encode($data);
            }
            if(!isset($post['data']['class_name']) && empty($post['data']['class_name'])){
                $data = array('code' => '1', 'msg' => '班级名称不能为空!', 'data' => []);
                return json_encode($data);
            }

            if(!isset($post['data']['class_desc']) && empty($post['data']['class_desc'])){
                $data = array('code' => '1', 'msg' => '班级描述不能为空!', 'data' => []);
                return json_encode($data);
            }
            if(!isset($post['data']['evaluate_period_time']) && empty($post['data']['evaluate_period_time'])){
                $data = array('code' => '1', 'msg' => '截至时间不能为空!', 'data' => []);
                return json_encode($data);
            }
            $exist = EvaluationTemplate::where('id',$post['data']['template_id'])->find();

            if(empty($exist)){
                $data = array('code' => '1', 'msg' => '该模板ID不存在!', 'data' => []);
                return json_encode($data);
            }
            if($exist->qr_code){
                $data = array('code' => '1', 'msg' => '该模板已生成二维码!', 'data' => []);
                return json_encode($data);
            }

            $post_data =[];
            $post_data['template_id'] = $post['data']['template_id'];
            $post_data['qr_user_id'] = $post['data']['qr_user_id'];
            $post_data['class_name'] = $post['data']['class_name'];
            $post_data['class_desc'] = $post['data']['class_desc'];
            $post_data['class_time'] = $post['data']['class_time'];
            $post_data['evaluate_period_time'] = $post['data']['evaluate_period_time'];
            $post_data['qr_source'] = 2;//自动
            $qr_id = EvaluationQrcode::insertGetId($post_data);
            $content = '班级名称:' .$post['data']['class_name']. PHP_EOL .'班级描述：' . $post['data']['class_desc'] . PHP_EOL . '上课时间：' .$post['data']['class_time']. PHP_EOL. '截至时间：' .$post['data']['evaluate_period_time'];
            list($qr_code,$value) = getCode($post['data']['template_id'], $qr_id, $content);
            $post_data['qr_code'] = $qr_code;
            $post_data['link_address'] = $value;
            $result = EvaluationQrcode::where('id',$qr_id)->update($post_data);
            if(!$result){
                $data = array('code' => '1', 'msg' => '操作失败!', 'data' => []);
            }else{
                $data = array('code' => '0', 'msg' => '操作成功!', 'data' => []);
            }
        }catch (\Exception $e){
            return json_encode(['code'=>1, 'msg'=> $e->getMessage()]);
        }


        return json_encode($data);
    }

    /**
     * Notes: 获取系统模板
     * author: bxm
     * Date: 2023/2/21
     * Time: 9:28
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTemplateList()
    {
        try{
            $this->yanzSigin(request()->post());
            $list = $this->model::order('id','desc')->field('id,name,fields,star_names,qr_code')->select(); //where('qr_code','<>', '')

            $res = array('code' => '0', 'msg' => '上传成功!', 'data' => $list);
        }catch (\Exception $e){
            return json_encode(['code'=>1, 'msg'=> $e->getMessage()]);
        }


        echo json_encode($res);
    }
}